<template>
  <view class="tabbar-box">
    <view class="menu-list">
      <view class="item" :style="{color:selIdx==index?hoverColor:color}" v-for="(item,index) in list"
            @click="click(index)" :key="index">
        <view :class="{
                        big:index==bigIdx
                    }">
          <view class="icon-box">
            <view class="bg" v-if="index==bigIdx"></view>
            <image class="img" :src="selIdx==index?item.selectedIconPath:item.iconPath">
            </image>
          </view>
        </view>
        <text class="label" v-if="item.text">{{ item.text }}</text>

      </view>
    </view>
  </view>

  <u-popup  :round="10" :show="publishVisible" @close="close">
    <view class="publish">
      <view class="title">选择发布的分类</view>
      <view class="type-list">
        <view @click="handleToPostPublish(item)" class="type-item" v-for="(item,index) of postTypeList" :key="index">
          <view class="iconfont_v2 icon-shuqian icon"></view>
          <view>{{item.name}}</view>
        </view>
      </view>
    </view>
  </u-popup>
</template>

<script>
import {POST_TYPE_LIST} from "@/constants";
export default {
  props: {
    list: {
      type: Array,
      default: [{
        "pagePath": "/pages/home",
        "iconPath": "/static/images/tabbar/home.png",
        "selectedIconPath": "/static/images/tabbar/home_.png",
        "text": "校园"
      }, {
        "pagePath": "/pages/find",
        "iconPath": "/static/images/tabbar/find.png",
        "selectedIconPath": "/static/images/tabbar/find_.png",
        "text": "发现"
      },
        {
          // "pagePath": "/pages/msg",
          "iconPath": "/static/images/tabbar/plus.png",
          "selectedIconPath": "/static/images/tabbar/plus.png",
          // "text": "搜搜"
        },
        {
          "pagePath": "/pages/msg",
          "iconPath": "/static/images/tabbar/msg.png",
          "selectedIconPath": "/static/images/tabbar/msg_.png",
          "text": "消息"
        }, {
          "pagePath": "/pages/my",
          "iconPath": "/static/images/tabbar/my.png",
          "selectedIconPath": "/static/images/tabbar/my_.png",
          "text": "我的"
        }
      ]
    },//菜单配置
    defaultSel: {
      type: Number,
      default: 0
    },//默认选中菜单
    hoverColor: {
      type: String,
      default: "#4484f0"
    },//选中颜色
    color: {
      type: String,
      default: "#666666"
    },//默认颜色
    bigIdx: {
      type: Number,
      default: -1
    },//大图标显示菜单-1为未开启
  },
  data() {
    return {
      postTypeList: POST_TYPE_LIST,
      selIdx: -1,
      oldSelIdx: null,
      publishVisible: false
    }
  },
  watch: {
    defaultSel: {
      handler(newVal, oldVal) {
        this.oldSelIdx = oldVal
      },
      immediate: true
    }
  },
  mounted() {
    this.selIdx = this.defaultSel;
  },
  methods: {
    handleToPostPublish(item) {

      this.publishVisible = false
      uni.navigateTo({
        url: '/pages/post/publish?postType=' + item.name
      })
    },
    close() {
      this.selIdx = this.oldSelIdx;
      this.publishVisible = false
    },
    click(idx) {
      if  (idx == this.selIdx) {
        return;
      }
      this.selIdx = idx;
      if (this.bigIdx === idx) {
        // 发布贴子
        console.log('发布贴子')
        this.publishVisible = true
        return;
      }
      if (this.list[idx].pagePath) {
        if (this.list[idx].isTabBar) {
          uni.switchTab({
            url: this.list[idx].pagePath
          });
        } else {
          uni.reLaunch({
            url: this.list[idx].pagePath
          });
        }
        return;
      }
      this.$emit("clickTab", idx)
    }
  }
}
</script>

<style scoped lang="scss">
.tabbar-box {
  position: fixed;
  bottom: 0;
  left: 0;
  z-index: 999;
  width: 100%;
  height: 130rpx;
  background-color: #ffffff;
  display: flex;
  align-items: center;
  //box-shadow: 0rpx 0rpx 12rpx rgba(0, 0, 0, 0.2);
  padding-bottom: 50rpx;
  padding-top: 40rpx;
}

.menu-list {
  display: flex;
  justify-content: space-around;
  text-align: center;
  font-size: 28rpx;
  width: 100%;
  align-items: center;
  .item {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    box-sizing: border-box;
    height: 100%;
    font-size: 22rpx;
    //color: #757575;

    image {
      width: 46rpx;
      height: 46rpx;
    }
    .label {
      margin-top: 6rpx;
    }

    .icon-box {
      width: 46rpx;
      height: 46rpx;
      text-align: center;
      display: flex;
      //justify-content: space-around;
      //position: relative;

      image {
        //flex: 80rpx 0 0;
        ////width: 160rpx;
        //height: 80rpx;
      }
    }

    .big {

      .icon-box {
        width: 130rpx;
        height: 130rpx;

        .bg {
          width: 130rpx;
          height: 130rpx;
          border-radius: 50%;
          position: relative;
          top: -20rpx;
          background-color: #fff;

        }
        image {
          width: 80rpx;
          height: 80rpx;
          position: absolute;
          top: 0rpx;
          left: 50%;
          transform: translateX(-50%);
          background-color: #ffffff;
          border-radius: 50%;
        }
      }
    }
  }


}

.publish {
  height: 500rpx;
  padding: 34rpx;
  -webkit-box-sizing: border-box;-moz-box-sizing: border-box;box-sizing: border-box;

  .title {
    text-align: center;
    font-weight: 700;
    margin-bottom: 30rpx;
  }
  .type-list {
    display: flex;
    flex-wrap: wrap;
    width: 100%;
    justify-content: space-between;
    .type-item {
      flex: 330rpx 0 0;
      color: #666464;
      background-color: #efefef;
      margin-bottom: 20rpx;
      border-radius: 12rpx;
      padding: 24rpx 20rpx;
      -webkit-box-sizing: border-box;-moz-box-sizing: border-box;box-sizing: border-box;

      display: flex;
      align-items: center;
      .icon {
        margin-right: 6rpx;
      }
    }
  }
}
</style>
